package main

import (
	"fmt"
	"my_code/biligoo/other"
	"sort"
	"sync"
	"time"
)

func monitorRefresh_Old() {
	go func() {
		for {
			roomids := getRoom()
			fmt.Printf("%v共获得房间数: %v        \n", other.TI(), len(roomids))
			//新增监控 指roomids里有，但bDmap里无
			for _, v := range roomids {
				bDlock.RLock()
				_, ok := bDmap[v]
				bDlock.RUnlock()
				if !ok {
					bDlock.Lock()
					bDmap[v] = &biDanmu{}
					bDlock.Unlock()
					//bDmap[v] = &biDanmu{}
					go danmu(v)
					time.Sleep(time.Second / 1000)
				}
			}
			//删除监控 指roomids里无，但bDmap里有
			bDlock.Lock()
			for k := range bDmap {
				ok := false
				for _, v := range roomids {
					if v == k {
						ok = true
					}
				}
				if !ok { //ok= 该bDmap的K在roomids中 else 不在roomids中
					bDmap[k].kill()
					delete(bDmap, k)
				}
			}
			bDlock.Unlock()
			/*for _, room := range roomids {
				danmu(room)
				time.Sleep(time.Second / 2)
			}*/
			time.Sleep(time.Second * 10)
		}
	}()
}

//aa:a有b无, bb:a有b有, cc:a无b有
func aabb(a, b []int) (aa, bb, cc []int) {
	// 创建一个映射用于快速检查 a 中的元素是否存在
	aMap := make(map[int]bool)
	for _, num := range a {
		aMap[num] = true
	}
	// 创建一个映射用于快速检查 b 中的元素是否存在
	bMap := make(map[int]bool)
	for _, num := range b {
		bMap[num] = true
	}

	for _, num := range a {
		if _, exists := bMap[num]; exists {
			// a 中的元素存在于 b 中
			bb = append(bb, num)
		} else {
			// a 中的元素不存在于 b 中
			aa = append(aa, num)
		}
	}
	for _, num := range b {
		if _, exists := aMap[num]; !exists {
			// b 中的元素不存在于 a 中
			cc = append(cc, num)
		}
	}

	return
}

var roomFromServer [][2]int = [][2]int{}
var roomFromServerLock *sync.RWMutex = &sync.RWMutex{}

func monitorRefresh() {
	go func() {
		for {
			time.Sleep(time.Second / 5)
			roomFromServerLock.Lock()
			sort.Slice(roomFromServer, func(i, j int) bool {
				return roomFromServer[i][1] > roomFromServer[j][1] // 按照第二个元素降序排序
			})
			roomFromServerLock.Unlock()

			for _, room := range roomFromServer {
				ok := danmu(room[0])
				if ok {
					break
				}
			}
		}
	}()
	go func() {
		for {
			roomidsOnline := <-monitorNewRoomChan
			roomids := []int{}
			for _, roomid := range roomidsOnline {
				roomids = append(roomids, roomid[0])
			}
			roomFromServerLock.Lock()
			roomFromServer = roomidsOnline
			roomFromServerLock.Unlock()
			oldRoomids := []int{}
			bDlock.RLock()
			for room, _ := range bDmap {
				//if b.roomid != 0 {
				oldRoomids = append(oldRoomids, room)
				//}
			}
			bDlock.RUnlock()
			//fmt.Println("新", len(roomids), "旧", len(oldRoomids))
			del, _, add := aabb(oldRoomids, roomids)
			//fmt.Println("增", len(add), "减", len(del))
			for _, room := range del {
				bDlock.Lock()
				bDmap[room].kill()
				delete(bDmap, room)
				bDlock.Unlock()
			}
			for _, room := range add {
				bDlock.Lock()
				bDmap[room] = &biDanmu{}
				bDlock.Unlock()
			}
			if len(add) != 0 || len(del) != 0 {
				fmt.Printf("%v监控房间增%v 删%v 总%v \n", other.TI(), len(add), len(del), len(roomids))
			}
			continue
			go func(rooms []int) {
				return
				maxWorkers := 5 // 最大并发goroutine数量

				var wg sync.WaitGroup
				semaphore := make(chan struct{}, maxWorkers)

				for _, room := range rooms {
					wg.Add(1)

					go func(room int) {
						defer wg.Done()

						semaphore <- struct{}{} // 获取一个信号量，控制并发数量
						danmu(room)
						<-semaphore // 释放信号量
					}(room)
				}

				wg.Wait()
				//fmt.Println(other.TI(), "All danmu processing completed.")

			}(add)
		}
	}()
}
